import smtplib,email.utils
from email.message import EmailMessage

smtp_server="smtp.qq.com"

from_addr="1807158216@qq.com"

# 需要在邮件里开通简单邮件服务协议IMAP/SMTP服务，使用授权码登录而不是邮箱密码
#授权码
password="yjqfdrtyeitiejjf"

to_addr="632690452@qq.com"

#--------------------------1.创建服务并登录-----------------------
conn=smtplib.SMTP_SSL(smtp_server,465)
conn.set_debuglevel(1)
conn.login(from_addr,password)

#---------------------------2.设置内容---------------------------
msg=EmailMessage()

msg['subject']='python测试'# 设置主题
msg['from']='蓝迷梦<%s>' % from_addr
msg['to']='蓝天迷梦<%s>' % to_addr
msg.set_content('来自Python的邮件','plain','utf-8')

#---------------------------添加附件--------------------------
imgid=email.utils.make_msgid()
with open('E:\\bg.jpg','rb') as img:
    msg.add_attachment(img.read(),maintype='image',subtype='jpg',filename='bg.jpg',cid=imgid)

#---------------------------3.发送-------------------------------
# conn.send_message(msg,from_addr, [to_addr])
conn.sendmail(from_addr,[to_addr],msg.as_string())
conn.quit()